package io.kiki.sba.registry.server.shared.providedata;

import java.util.ArrayList;
import java.util.Collection;


public class SystemPropertyProcessorManager {

    private final Collection<AbstractFetchSystemPropertyService> systemDataProcessors = new ArrayList<>();

    private final Collection<AbstractFetchPersistenceSystemProperty> systemDataPersistenceProcessors = new ArrayList<>();

    public void addSystemDataProcessor(AbstractFetchSystemPropertyService systemDataProcessor) {
        systemDataProcessors.add(systemDataProcessor);
    }

    public void addSystemDataPersistenceProcessor(AbstractFetchPersistenceSystemProperty systemDataProcessor) {
        systemDataPersistenceProcessors.add(systemDataProcessor);
    }

    public boolean doFetch(String dataInfoId) {
        for (FetchSystemPropertyService systemDataProcessor : systemDataProcessors) {
            if (systemDataProcessor.support(dataInfoId)) {
                return systemDataProcessor.doFetch();
            }
        }
        for (FetchSystemPropertyService systemDataProcessor : systemDataPersistenceProcessors) {
            if (systemDataProcessor.support(dataInfoId)) {
                return systemDataProcessor.doFetch();
            }
        }

        return false;
    }

    public boolean startFetchMetaSystemProperty() {
        boolean success = true;

        for (AbstractFetchSystemPropertyService systemDataProcessor : systemDataProcessors) {
            if (!systemDataProcessor.start()) {
                success = false;
            }
        }
        return success;
    }

    public boolean startFetchPersistenceSystemProperty() {
        boolean success = true;

        for (AbstractFetchPersistenceSystemProperty systemDataProcessor : systemDataPersistenceProcessors) {
            if (!systemDataProcessor.start()) {
                success = false;
            }
        }
        return success;
    }
}
